﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InPatientController;

namespace HPMDesktop.Patient
{
    public partial class frmWardAllocation : Form
    {
        private Form _objParentForm;
        private string _parentClass;
        private string _patientName;
        private string _CRNumber;
        private const int MAX_BEDS_TO_DISPLAY = 5;
        private DataSet _wardDataSet;
        private DataSet[] _bedDataSet;

        public frmWardAllocation(Form objParentForm,
                                 string parentClass,
                                 string patientName,
                                 string CRNumber)
        {
            InitializeComponent();
            _objParentForm = objParentForm;
            _parentClass = parentClass;
            _patientName = patientName;
            _CRNumber = CRNumber;
        }

        private void frmWardAllocation_Load(object sender, EventArgs e)
        {
            txtpname.Text = _patientName;
            txtCRNum.Text = _CRNumber;

            setupDataGridView();
            updateDataGridView();
        }

        private void setupDataGridView()
        {
            dataGridBeds.AutoSize = true;
            dataGridBeds.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dataGridBeds.MultiSelect = false;
            dataGridBeds.ScrollBars = ScrollBars.Both;
            dataGridBeds.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dataGridBeds.AllowUserToResizeColumns = true;
            dataGridBeds.Enabled = true;
            dataGridBeds.EditMode = DataGridViewEditMode.EditProgrammatically;

            dataGridBeds.Columns.Add("WardName", "Ward Name");
            dataGridBeds.Columns.Add("BedsAvailable", "Beds Available");
            for (int indx = 1; indx <= MAX_BEDS_TO_DISPLAY; indx++)
            {
                dataGridBeds.Columns.Add("Bed" + indx, "Bed " + indx.ToString());
            }
        }

        private void updateDataGridView()
        {
            WardBed_DataLoader objDataLoader = new WardBed_DataLoader();
            _wardDataSet = objDataLoader.Get_All_Wards();
            
            if (_wardDataSet != null)
            {
                int rowIndx = 0;
                _bedDataSet = new DataSet[_wardDataSet.Tables[0].Rows.Count];

                foreach (DataRow wardRow in _wardDataSet.Tables[0].Rows)
                {
                    _bedDataSet[rowIndx] = objDataLoader.Get_Available_Beds(wardRow["WardId"].ToString());

                    if (_bedDataSet != null)
                    {
                        dataGridBeds.Rows.Add();
                        dataGridBeds.Rows[rowIndx].Cells["WardName"].Value = wardRow["WardName"].ToString();
                        dataGridBeds.Rows[rowIndx].Cells["WardName"].ToolTipText = "Number of Beds : " + wardRow["NumberOfBeds"];
                        if (wardRow["Location"] != null)
                        {
                            dataGridBeds.Rows[rowIndx].Cells["WardName"].ToolTipText += "\nLocation : " + wardRow["Location"];
                        }
                        dataGridBeds.Rows[rowIndx].Cells["BedsAvailable"].Value = _bedDataSet[rowIndx].Tables[0].Rows.Count.ToString();
                        int bedIndx = 1;
                        foreach (DataRow bedRow in _bedDataSet[rowIndx].Tables[0].Rows)
                        {
                            dataGridBeds.Rows[rowIndx].Cells["Bed" + bedIndx].Value = bedRow["BedNumber"].ToString();
                            bedIndx++;
                            if (bedIndx > MAX_BEDS_TO_DISPLAY)
                            {
                                break;
                            }
                        }
                    }
                    rowIndx++;
                }
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            int rowIndex = dataGridBeds.SelectedCells[0].RowIndex;
            int columnIndex = dataGridBeds.SelectedCells[0].ColumnIndex;

            if (rowIndex == (dataGridBeds.Rows.Count -1 )||
                columnIndex < 2 ||
                columnIndex > (1 + Convert.ToInt32(dataGridBeds.Rows[rowIndex].Cells["BedsAvailable"].Value.ToString())))
            {
                MessageBox.Show("Please select a bed.");
                return;
            }

            string WardId = _wardDataSet.Tables[0].Rows[rowIndex]["WardId"].ToString();
            string BedId = _bedDataSet[rowIndex].Tables[0].Rows[columnIndex - 2]["BedId"].ToString();

            DateTime date = new DateTime(dtStartDate.Value.Year,
                                         dtStartDate.Value.Month,
                                         dtStartDate.Value.Day,
                                         tmStartTime.Value.Hour,
                                         tmStartTime.Value.Minute,
                                         tmStartTime.Value.Second);

            PatientWard_Mapper objMapper = new PatientWard_Mapper();
            PatientWard objPatientWard = new PatientWard(_CRNumber, WardId, BedId, date);
            if (objMapper.Load_PatientWard_DATA(objPatientWard))
            {
                if (_parentClass.Equals("frmPatientDetails"))
                {
                    ((frmPatientDetails)_objParentForm).updateWardBedHistoryView();
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("Error in allocating new bed. Please try again.");
            }
        }
    }
}
